// hooks/useAppPermission.ts
import { usePermission, createPermissionDirective } from './usePermission';

// 创建应用级别的权限实例
export const useAppPermission = () => {
  const permission = usePermission({
    strict: true,
    validate: (permission, user) => {
      // 自定义验证逻辑
      if (user.role === 'admin' && permission === 'system:delete') {
        return false; // 管理员也不能删除系统数据
      }
      return true;
    }
  });

  // 初始化权限映射
  const initPermissionMap = () => {
    permission.addRolePermissions('guest', ['read']);
    permission.addRolePermissions('user', ['read', 'create', 'update']);
    permission.addRolePermissions('admin', ['read', 'create', 'update', 'delete', 'system:manage']);
    permission.addRolePermissions('super-admin', ['*']);
  };

  // 权限指令
  const vPermission = createPermissionDirective(permission);

  return {
    ...permission,
    initPermissionMap,
    vPermission
  };
};

// 导出单例实例
export const appPermission = useAppPermission();